<?php
include_once('./config/config.php');
$admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
  setcookie('auth', '', time() - 1);
  notice('退出成功', 'login.php');
}

// 判断是否有表单提交
if ($_POST) {
  // 接收参数
  $username = isset($_POST['username']) ? trim($_POST['username']) : '';
  $password = isset($_POST['password']) ? trim($_POST['password']) : '';
  // 判断参数是否为空
  if (empty($username)) {
    notice('用户名不能为空');
  }
  if (empty($password)) {
    notice('密码不能为空');
  }
  // 生成密码盐
  $salt = build_randstr();
  // 拼接加密
  $password = md5($password . $salt);
  // 调用封装上传函数
  $avatar = build_upload('avatar');
  if ($avatar['code'] === 0) {
    notice($avatar['msg']);
  }
  // 组装数据
  $data = [
    'username' => $username,
    'password' => $password,
    'salt' => $salt,
    'avatar' => $avatar['data']
  ];
  $result = add('admin', $data);
  if ($result['code'] === 1) {
    notice('添加管理员成功', 'AdminList.php');
  } else {
    is_file('.' . $avatar) &&  unlink('.' . $avatar);

    echo $result;
    exit;
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>

  <!-- 公共样式 -->
  <?php include_once('./common/meta.php'); ?>
  <style>
    #upload {
      width: 214px;
      height: 200px;
      object-fit: cover;
      cursor: pointer;
    }
  </style>

</head>

<body>
  <!-- 引入头部 -->
  <?php include_once('common/header.php'); ?>

  <!-- 侧边菜单栏 -->
  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加管理员</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="AdminList.php">AdminList</a> <span class="divider">/</span></li>
      <li class="active">AdminAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='AdminList.php'"><i class="icon-list"></i> 管理员列表</button>
          <div class="btn-group"></div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <!-- 用户名 -->
                <label>用户名</label>
                <input type="text" name="username" id="username" placeholder="请输入用户名" class="input-xxlarge">
                <span id="username-error"></span>

                <label>密码</label>
                <input type="password" name="password" placeholder="请输入密码" class="input-xxlarge">

                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden class="input-xxlarge">
                <img src="./assets/images/upload.png" id="upload" alt="">

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('common/footer.php'); ?>
      </div>
    </div>
  </div>
</body>

</html>

<?php include_once('common/script.php'); ?>

<script>
  // 图片上传
  $('#upload').click(function() {
    $('#avatar').click();
  })

  $('#avatar').change(function() {
    let file = this.files[0] ?? '';
    if (file) {
      let Reader = new FileReader();
      Reader.readAsDataURL(file);
      Reader.onload = function() {
        $('#upload').attr('src', Reader.result);
      }
    }
  })

  // 检查用户名是否可用
  $('#username').change(function() {
    let username = $.trim($(this).val());

    if (!username) {
      return false;
    }

    // 发起请求
    $.ajax({
      type: 'post',
      url: 'api.php',
      data: {
        username,
        action: 'select_admin_username'
      },
      dataType: 'json',
      success: (result) => {
        if (result.code === 1) {
          $('#username-error').text(result.msg).css({
            color: 'green',
            fontSize: '16px'
          })
        } else {
          $('#username-error').text(result.msg).css({
            color: 'red',
            fontSize: '16px'
          })
        }
      }
    })
  })
</script>